﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BLL;
using Entities;
using System.Text.RegularExpressions;
using System.Data.Linq.Mapping;

namespace QLTB_CTK34
{
   
    public partial class FrmQuanLytaiKhoan : Form
    {
        public FrmQuanLytaiKhoan()
        {
            InitializeComponent();
            dgvDSTaiKHoan.AutoGenerateColumns = false;
        }
        /// <summary>
        /// Load Tài Khoản
        /// </summary>
        public void LoadData()
        {
            dgvDSTaiKHoan.DataSource = QLTaiKhoan.GetAll();
        }

        private void FormQuanLytaiKhoan_Load(object sender, EventArgs e)
        {
            LoadData();
            if (dgvDSTaiKHoan.Rows.Count > 0)
            {
                dgvDSTaiKHoan.Rows[0].Selected = true;
            }
            cbbLoaiTK.SelectedIndex = 0;
            LoadPhanQuyen();
        }

        /// <summary>
        /// Thêm Tài Khoản
        /// </summary>
        private void tsbtnThemTK_Click(object sender, EventArgs e)
        {
            FormThemTaiKhoan frmthemtk = new FormThemTaiKhoan();
            if (frmthemtk.ShowDialog(this) == DialogResult.OK)
            {
                LoadData();
                Generic.SelectDataGridviewRowTheoMa(dgvDSTaiKHoan, "MaTaiKhoan", frmthemtk.TaiKhoanThem.MaTK);
            }
        }

        string MaTk= "";
        /// <summary>
        /// Xóa Tài Khoản
        /// </summary>
        private void tsbtnXoaTK_Click(object sender, EventArgs e)
        {
            for (int i = dgvDSTaiKHoan.SelectedRows.Count - 1; i >= 0; i--)
                QLTaiKhoan.FindByID(dgvDSTaiKHoan.SelectedRows[i].Cells["MaTaiKhoan"].Value.ToString());
            LoadData();
			QLNhatKy.ThemNhatKy("Xóa tài khoản");
        }

        /// <summary>
        /// Lấy Danh Sách Mã Tài Khoản
        /// </summary>
        private List<string> LayDSMaTK(List<string> dstk)
        {
            for(int i = 0 ;i<dgvDSTaiKHoan.SelectedRows.Count;i++)
                dstk.Add(dgvDSTaiKHoan.SelectedRows[i].Cells[0].Value.ToString());
            return dstk;
        }

        /// <summary>
        /// Click Datagridview
        /// </summary>
        private void dtgvQLTK_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
                MaTk = dgvDSTaiKHoan.Rows[e.RowIndex].Cells[0].Value.ToString();
            else if (e.ColumnIndex >= 0)
                QLTaiKhoan.XapXep(dgvDSTaiKHoan, e);
        }

        private void FormQuanLytaiKhoan_FormClosed(object sender, FormClosedEventArgs e)
        {
            LoadData();
        }

        /// <summary>
        /// Cập Nhập Tài Khoản
        /// </summary>
        private void tsbtnCapNhapTk_Click(object sender, EventArgs e)
        {
            if (dgvDSTaiKHoan.SelectedRows.Count > 0)
            {
                string matksua = dgvDSTaiKHoan.SelectedRows[0].Cells["MaTaiKhoan"].Value.ToString();
                TaiKhoan tksua = QLTaiKhoan.FindByID(matksua);
                FrmCapNhapTaiKhoan frmcntk = new FrmCapNhapTaiKhoan(tksua);
                if (frmcntk.ShowDialog() == DialogResult.OK)
                {
                    LoadData();
                    Generic.SelectDataGridviewRowTheoMa(dgvDSTaiKHoan, "MaTaiKhoan", frmcntk._taikhoan.MaTK);
                }
            }
        }

        /// <summary>
        /// Tiềm Kiếm Tài Khoản
        /// </summary>
        private void txtTimKiem_TextChanged(object sender, EventArgs e)
        {
            QLTaiKhoan.DKTimKiem dk = (QLTaiKhoan.DKTimKiem)cbbLoaiTK.SelectedIndex;
            QLTaiKhoan.TimKiem(dgvDSTaiKHoan,cbbLoaiTK,dk,txtTimKiem.Text);
            
        }
        /// <summary>
        /// Menutooltrip
        /// </summary>
        private void thêmTàiKhoảnToolStripMenuItem_Click(object sender, EventArgs e)
        {
            toolStripButtonThem.PerformClick();
        }

        private void cậpNhậpTàiKhoảnToolStripMenuItem_Click(object sender, EventArgs e)
        {
            toolStripButtonSua.PerformClick();
        }

        private void xóaTàiKhoảnToolStripMenuItem_Click(object sender, EventArgs e)
        {
            toolStripButtonXoa.PerformClick();
        }

        private void thoátToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

		private void tsbtnRefresh_Click(object sender, EventArgs e)
		{
			LoadData();
		}

        private void toolStripButtonPrint_Click(object sender, EventArgs e)
        {
            FrmInAn inan = new FrmInAn(DKForm.taikhoan);
            inan.ShowDialog();
        }
        void LoadPhanQuyen()
        {
            toolStripButtonThem.Visible = thêmTàiKhoảnToolStripMenuItem.Visible = PhanQuyen.CheckQuyenSuDung(MyAccount.account.MaTK, "ThemTK");
            toolStripButtonSua.Visible = cậpNhậpTàiKhoảnToolStripMenuItem.Visible = PhanQuyen.CheckQuyenSuDung(MyAccount.account.MaTK, "SuaTK");
            toolStripButtonXoa.Visible = xóaTàiKhoảnToolStripMenuItem.Visible = PhanQuyen.CheckQuyenSuDung(MyAccount.account.MaTK, "XoaTK");
            toolStripButtonPrint.Visible = PhanQuyen.CheckQuyenSuDung(MyAccount.account.MaTK, "InTK");
        }

		private void dgvDSTaiKHoan_MouseDown(object sender, MouseEventArgs e)
		{
			if (e.Button == MouseButtons.Right)
			{
				var a = dgvDSTaiKHoan.HitTest(e.X, e.Y);
				dgvDSTaiKHoan.ClearSelection();
				dgvDSTaiKHoan.Rows[a.RowIndex].Selected = true;
			}
		}
    }
}
